<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="formRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="会员卡号" name="code">
						<a-input v-model:value="searchFormState.code" allow-clear></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="会员名称" name="nickName">
						<a-input v-model:value="searchFormState.nickName" allow-clear></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="会员等级" name="memberRank">
						<a-select
							v-model:value="searchFormState.memberRank"
							:options="memberRankOptions"
							placeholder="请选择类型"
							allow-clear
						></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="手机号" name="phone">
						<a-input v-model:value="searchFormState.phone" allow-clear></a-input>
					</a-form-item>

				</a-col>
			</a-row>
			<a-row style="padding-bottom: 24px">
				<a-col :span="6">
					<a-button type="primary" html-type="submit" @click="table.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => formRef.resetFields()">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:scroll="{ x: 1500 }"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="form.onOpen()">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<a-button danger @click="removeBatch()">
						删除
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'userInfo'">
					<div>{{ record.nickName }}</div>
					<div>用户ID：{{record.userId}}</div>
					<div v-if="record.id > 0">卡ID：{{record.id}}</div>
					<div v-if="record.code">
						<div>卡号：{{ record.code}}</div>
						<div v-if="record.wxCode">微信卡号：{{ record.wxCode}}</div>
					</div>
					<div v-if="record.phone">
						手机号：{{record.phone}}
					</div>
					<div v-else>
						<a-tag>未开卡</a-tag>
					</div>
					<a-tag v-if="record.memberRank === '1'" color="blue">{{ record.memberRankName }}</a-tag>
					<a-tag v-if="record.memberRank === '2'" color="green">{{ record.memberRankName }}</a-tag>
					<a-tag v-if="record.memberRank === '3'" color="red">{{ record.memberRankName }}</a-tag>
				</template>
				<template v-if="column.dataIndex === 'wxInfo'">
					<div>关注状态：{{record.subscribe === '1' ? '已关注' : '未关注'}}</div>
					<div v-if="record.subscribe === '1'">关注时间：{{record.subscribeTime}}</div>
					<div v-else-if="record.subscribe === '0' && !record.cancelSubscribeTime">取关时间：{{record.cancelSubscribeTime}}</div>
					<div v-if="record.subscribe === '1'">关注渠道：{{$TOOL.dictTypeData('SUBSCRIBE_SCENE', record.subscribeScene)}}</div>
				</template>
				<template v-if="column.dataIndex === 'icon'">
					<component :is="record.icon" />
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-button type="link" size="small" @click="form.onOpen(record)">编辑</a-button>
					<a-divider type="vertical" />
					<a-button type="link" size="small" @click="adjustAccountForm.onOpen(record)">调账</a-button>
					<a-divider type="vertical" />
					<a-dropdown :trigger="['click']">
						<a-button size="small" type="link" @click.prevent>
							更多
							<DownOutlined />
						</a-button>
						<template #overlay>
							<a-menu>
								<a-menu-item>
									<a-button :disabled="!record.id" type="link" size="small" @click="setMemberRank(memberRankForm, record)">设置会员等级</a-button>
								</a-menu-item>
								<a-menu-item>
									<a-button :disabled="!record.id" type="link" size="small" @click="handleRecharge(voucherForm, record)">充值</a-button>
								</a-menu-item>
								<a-menu-item>
									<a-button :disabled="!record.id" type="link" size="small" @click="adjustBonusForm.onOpen(record)">调积分</a-button>
								</a-menu-item>
								<a-menu-item>
									<a-button type="link" size="small" @click="sendMessage(messageForm, record)">消息发送</a-button>
								</a-menu-item>
								<a-menu-item>
									<a-popconfirm title="确定要删除此会员吗？" @confirm="remove(record)">
										<a-button type="link" danger size="small">删除</a-button>
									</a-popconfirm>
								</a-menu-item>
							</a-menu>
						</template>
					</a-dropdown>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="form" @successful="table.refresh(true)" />
	<AdjustAccountForm ref="adjustAccountForm" @successful="table.refresh(true)" />
	<AdjustBonusForm ref="adjustBonusForm" @successful="table.refresh(true)" />
	<VoucherForm ref="voucherForm" @successful="table.refresh(true)"/>
	<MemberRankForm ref="memberRankForm" @successful="table.refresh(true)"/>
	<SendMessageForm ref="messageForm" @successful="table.refresh(false)"/>
</template>

<script setup name="crmMember">
	import { message } from 'ant-design-vue'
	import crmApi from '@/api/crm/crmApi'
	import MemberRankForm from './MemberRankForm.vue'
	import VoucherForm from '@/views/crm/voucher/form.vue'
	import AdjustAccountForm from './adjustAccountForm.vue'
	import AdjustBonusForm from './adjustBonusForm.vue'
	import SendMessageForm from './SendMessageForm.vue'
	import Form from './form.vue'
	import tool from '@/utils/tool'
	let searchFormState = reactive({})
	const formRef = ref()
	const table = ref(null)
	let form = ref()
	let messageForm =ref()
	let voucherForm = ref()
	let memberRankForm = ref()
	let adjustAccountForm = ref()
	let adjustBonusForm = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const columns = [
		{
			title: '客户',
			dataIndex: 'userInfo',
		},
		{
			title: '公众号',
			dataIndex: 'wxInfo'
		},
		{
			title: '储值余额',
			dataIndex: 'amount',
			width: 100,
			align: 'center'
		},
		{
			title: '储值本金',
			dataIndex: 'amountRecharge',
			width: 100,
			align: 'center'
		},
		{
			title: '储值赠送',
			dataIndex: 'amountPresent',
			width: 100,
			align: 'center'
		},
		{
			title: '可用积分',
			dataIndex: 'bonus',
			width: 100,
			align: 'center'
		},
		{
			title: '消费次数',
			dataIndex: 'saleFrequency',
			width: 100,
			sorter: true,
			align: 'center'
		},
		{
			title: '累计消费金额',
			dataIndex: 'totalSaleAmount',
			width: 120,
			sorter: true,
			align: 'center'
		},
		{
			title: '创建时间',
			dataIndex: 'createdTime',
			ellipsis: true,
			sorter: true,
			align: 'center',
			width: 180
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			fixed: 'right'
		}
	]

	let subscribeSceneOptions = tool
		.dictTypeList('SUBSCRIBE_SCENE')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})

	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	let memberRankOptions = tool
		.tableList('CRM_MEMBER_RANK')
		.map((item) => {
			return {
				value: item['id'],
				label: item['name']
			}
		})
	// 列表数据
	const loadData = (parameter) => {
		return crmApi.memberPage(Object.assign(parameter, searchFormState)).then((res) => {
			return res
		})
	}
	const setMemberRank = (form, record) => {
		form.onOpen(record)
	}
	const sendMessage = (form, record) => {
		form.onOpen(record)
	}
	const handleRecharge = (form, record) => {
		if (!record.id) {
			message.error('未开卡，不允许充值')
		} else {
			record.cardType = 'MEMBER'
			form.onOpen(record)
		}
	}
	// 删除
	const remove = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		crmApi.memberDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const removeBatch = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		crmApi.memberDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
</script>

<style scoped>

</style>
